import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-11
 * Time: 22:07
 */
public class MyStack {
    int size = 0;
    int[] arr;
    //构造一个空的栈
    public MyStack() {
        this.arr = new int[10];
    }

    //将data入栈，并返回data
    public int push(int data) {
        //判满，满了进行扩容
        if (isFull()) {
            arr = Arrays.copyOf(arr,2*arr.length);
        }
        this.arr[this.size] = data;
        this.size++;
        return data;
    }
    //判断栈满了没有
    private boolean isFull() {
        if (this.size == arr.length) {
            return true;
        }else {
            return false;
        }
    }

    //将栈顶元素出栈并返回
    public int pop() {
       isEmpty();
        int ans = this.arr[this.size - 1];
        this.size--;
        return ans;
    }
    //判断栈是否为空,为空则抛出异常
    private void isEmpty() {
        if (this.size == 0) {
            throw new StackEmptyException("当前栈为空！");
        }
    }

    //获取栈顶元素
    public int peek() {
        isEmpty();
        int ans = this.arr[this.size - 1];
        return ans;
    }

    //获取栈中有效元素个数
    public int size() {
        return this.size;
    }

    //检测栈是否为空
    public boolean empty() {
        return this.size == 0;
    }
}
